package laf.startup;

import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.core.Runtime;
import jade.tools.rma.rma;
import jade.wrapper.AgentController;
import jade.wrapper.ContainerController;
import jade.wrapper.StaleProxyException;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import laf.agents.CognitiveAgent;
import laf.agents.LanguageSymbolDictionary;
import laf.behaviours.CollectGroundingHistory;
import laf.env.Attribute;
import laf.env.AttributeDomain;
import laf.env.AttributeValue;
import laf.env.EnvObject;
import laf.env.Enviroment;
import laf.env.State;
import laf.lang.Symbol;
import laf.learning.GroundingStrategyImpl;
import laf.learning.KESGroundingRule;
import laf.learning.LocalGroundingRule;
import laf.learning.ReactiveSymbolGroundingBehaviour;
import laf.learning.StochasticApproximationGroundingRule;
import laf.time.TimeManager;
import laf.ui.CorrelationChart;

public class WindDirectionStartUp {
	
	private static final long TICK_DURATION = 1000;

	public static void main(String[] args) {
		
		Logger.getLogger("laf").setLevel(Level.WARNING);
		TimeManager tm = TimeManager.getInstance();
		tm.setTickDuration(TICK_DURATION);
		Enviroment env = Enviroment.getInstance();
		
		AttributeDomain domain8 = Enviroment.createDomain("0-7", 0, 7);
		
		Attribute windDirection = Enviroment.createAttribute("wind-direction", domain8);
		
		for (int i = 1; i < 5; i++) {
			Enviroment.createObject("o" + i);
		}
		
		List<Symbol> symbols = new ArrayList<Symbol>();
		Symbol sN = new Symbol("N");
		symbols.add(sN);
		Symbol sNE = new Symbol("NE");
		symbols.add(sNE);
		Symbol sE = new Symbol("E");
		symbols.add(sE);
		Symbol sSE = new Symbol("SE");
		symbols.add(sSE);
		
		Symbol sS = new Symbol("S");
		symbols.add(sS);
		Symbol sSW = new Symbol("SW");
		symbols.add(sSW);
		Symbol sW = new Symbol("W");
		symbols.add(sW);
		Symbol sNW = new Symbol("NW");
		symbols.add(sNW);
		
		LanguageSymbolDictionary dictionary = new LanguageSymbolDictionary(symbols, env.getAttributes());
		LanguageSymbolDictionary.createCorrelation(dictionary, sN, windDirection.getValue(0), windDirection.getValue(0), 1, Long.MAX_VALUE);
		LanguageSymbolDictionary.createCorrelation(dictionary, sNE, windDirection.getValue(1), windDirection.getValue(1), 1, Long.MAX_VALUE);
		LanguageSymbolDictionary.createCorrelation(dictionary, sE, windDirection.getValue(2), windDirection.getValue(2), 1, Long.MAX_VALUE);
		LanguageSymbolDictionary.createCorrelation(dictionary, sSE, windDirection.getValue(3), windDirection.getValue(3), 1, Long.MAX_VALUE);
		
		LanguageSymbolDictionary.createCorrelation(dictionary, sS, windDirection.getValue(4), windDirection.getValue(4), 1, Long.MAX_VALUE);
		LanguageSymbolDictionary.createCorrelation(dictionary, sSW, windDirection.getValue(5), windDirection.getValue(5), 1, Long.MAX_VALUE);
		LanguageSymbolDictionary.createCorrelation(dictionary, sW, windDirection.getValue(6), windDirection.getValue(6), 1, Long.MAX_VALUE);
		LanguageSymbolDictionary.createCorrelation(dictionary, sNW, windDirection.getValue(7), windDirection.getValue(7), 1, Long.MAX_VALUE);
		
		LanguageSymbolDictionary copy = new LanguageSymbolDictionary(dictionary);
		
		Runtime jadeRT = Runtime.instance();
		Profile profile = new ProfileImpl();
		ContainerController containerCntr = jadeRT.createMainContainer(profile);
		try {
			Object[] arguments = { copy };
			AgentController agentCntr = containerCntr.createNewAgent("CA1", CognitiveAgent.class.getName(), arguments);
			agentCntr.start();
			
			CognitiveAgent newAgent = new CognitiveAgent();
			newAgent.addBehaviour(new ReactiveSymbolGroundingBehaviour(newAgent, new GroundingStrategyImpl(new KESGroundingRule())));
			newAgent.addBehaviour(new CollectGroundingHistory(newAgent, 1, 9));
			agentCntr = containerCntr.acceptNewAgent("NA", newAgent);
			agentCntr.start();
			
			//agentCntr = containerCntr.createNewAgent("RMA", rma.class.getName(), null);
			//agentCntr.start();
			
			CorrelationChart chart = new CorrelationChart();
			chart.setGroundingHistory(newAgent.getGroundingHistory());
			chart.setVisible(true);
		} catch (StaleProxyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		tm.start();
		
		//System.out.println(dictionary);
	}

}
